<?php

class VerifyImage
{
    CONST SESSEION_PREFIX = 'verifyImage_';
    CONST CODE_NUM = 4;    
    public static $codeset = "abcdefghjkmnpqrstuvwxyz123456789";

    public static function getSessionName($name)
    {
        return self::SESSEION_PREFIX . $name;
    }
    
    public static function create(&$code)
    {
        $im = imagecreate(60,25);
        $back = ImageColorAllocate($im, 245,245,245);
        imagefill($im,0,0,$back); //背景
        
        for($i=0;$i<self::CODE_NUM;$i++){
            $char = self::$codeset[mt_rand(0, strlen(self::$codeset)-1)];
            $code.= $char;
            $font = ImageColorAllocate($im, mt_rand(100,255),mt_rand(0,100),mt_rand(100,255));
            imagestring($im, 5, 5+$i*14, 5, $char, $font);
        }
        
        for($i=0;$i<100;$i++) //加入干扰象素
        { 
            $randcolor = ImageColorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
            imagesetpixel($im, mt_rand()%70 , mt_rand()%30 , $randcolor);
        }
        
        return $im;
    }
    
    public static function delete(&$image)
    {
        ImageDestroy($image);    
    }
}

?>
